<template>
  <div class="app-container">
    <div class="info">
       <span>用户下订后系统自动记录当前退订规则信息，修改退订规则不会影响已生成订单信息</span>
    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['apartment:rule:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="unsubscribeRuleName">
        <el-input
          v-model="queryParams.unsubscribeRuleName"
          placeholder="请输入规则名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="unsubscribeState">
        <el-select
          v-model="queryParams.unsubscribeState"
          placeholder="请选择"
          clearable
        >
          <el-option label="显示" value="0" />
          <el-option label="隐藏" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="unsubscribeRuleList"
      @selection-change="handleSelectionChange"
      ref="table"
    >
      <el-table-column label="名称" align="center" prop="unsubscribeRuleName" />
      <el-table-column label="规则" align="center" prop="unsubscribeRuleType">
        <template slot-scope="scope">
          {{ scope.row.unsubscribeType == 1 ? "严格模式" : "酒店模式" }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="">
        <template slot-scope="scope">
          {{ scope.row.unsubscribeState == 0 ? "显示" : "隐藏" }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="unsubscribeRemark">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleStatusChange(scope.row)"
            v-hasPermi="['apartment:rule:edit']"
            >{{ scope.row.unsubscribeState != 0 ? "显示" : "隐藏" }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['apartment:rule:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            style="color: #ef0c2e"
            @click="handleDelete(scope.row)"
            v-hasPermi="['apartment:rule:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <addRule v-model="addRuleVisible" :unsubscribeRuleId="id" :title="title" @input="getList" />
  </div>
</template>

<script>
import {
  listUnsubscribeRule,
  addUnsubscribeRule,
  delUnsubscribeRule,
  changeUnsubscribeRuleStatus,
  getUnsubscribeRule,
  updateUnsubscribeRule,
} from "@/api/house/rule";
import addRule from "./components/add-rule.vue";

export default {
  name: "Rule",
  dicts: ["dict_unsubscribe_rule_type"],
  components: {
    addRule,
  },
  data() {
    // freeDays: undefined,
    //     minDays: undefined,
    //     maxDays: undefined,
    //     rent: undefined,
    //     cashPledge: undefined,
    //     todayRent: undefined,
    //     todayCashPledge: undefined,
    var validateCancelType = (rule, value, callback) => {
      if (value === "1") {
        callback();
      } else {
        if (
          !this.form.freeDays ||
          !this.form.minDays ||
          !this.form.maxDays ||
          !this.form.rent ||
          !this.form.cashPledge ||
          !this.form.todayRent ||
          !this.form.todayCashPledge
        ) {
          callback(new Error("数据不能为空"));
        }
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退订规则表格数据
      unsubscribeRuleList: [],
      //退订规则详情数据
      unsubscribeRuleData: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //详情弹窗
      detailVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unsubscribeRuleName: undefined,
        unsubscribeRuleType: undefined,
      },
      // 表单参数
      form: {},
      id: undefined,
      addRuleVisible: false,
      // 表单校验
      rules: {
        unsubscribeRuleName: [
          { required: true, message: "规则名称不能为空", trigger: "blur" },
        ],
        unsubscribeRuleType: [
          { required: true, message: "适用范围不能为空", trigger: "blur" },
        ],
        cancelType: [
          { required: true, message: "取消类型不能为空", trigger: "blur" },
          { validator: validateCancelType, trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listUnsubscribeRule(this.queryParams)
        .then((response) => {
          this.unsubscribeRuleList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        unsubscribeRuleId: undefined,
        unsubscribeRuleName: undefined,
        unsubscribeRuleType: "1",
        cancelType: "1",
        freeDays: undefined,
        minDays: undefined,
        maxDays: undefined,
        rent: undefined,
        cashPledge: undefined,
        todayRent: undefined,
        todayCashPledge: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.unsubscribeRuleId);
      this.single = selection.length != 1;
      console.log("single", this.single);
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.id = '';
      this.addRuleVisible = true;
      this.title = "新增规则";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.id = row.unsubscribeRuleId;
      this.addRuleVisible = true;
      this.title = "编辑规则";
    },
    handleDetail(row) {
      // this.reset();
      // const unsubscribeRuleId = row.unsubscribeRuleId || this.ids;
      // getUnsubscribeRule(unsubscribeRuleId).then((response) => {
      //   this.unsubscribeRuleData = { ...response.data };
      //   this.detailVisible = true;
      //   this.title = "详情";
      // });
      this.id = row.unsubscribeRuleId;
      this.title = "详情";
      this.detailVisible = true;
    },
    //检测新增/修改提交取消规则数据
    checkCancel() {},
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.unsubscribeRuleId != undefined) {
            updateUnsubscribeRule(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnsubscribeRule(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const unsubscribeRuleIds = row.unsubscribeRuleId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除名称为"' + row.unsubscribeRuleName + '"的数据项？'
        )
        .then(function () {
          return delUnsubscribeRule(unsubscribeRuleIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },
    // 退订规则状态修改
    handleStatusChange(row) {
      this.$modal
        .confirm('确认修改名称为"' + row.unsubscribeRuleName + '"的状态？')
        .then(function () {
          return changeUnsubscribeRuleStatus(row.unsubscribeRuleId);
        })
        .then(() => {
          this.$modal.msgSuccess("操作成功");
          this.getList();
        })
        .catch(function () {});
    },
    //请空选择项
    clearSelect() {
      this.$refs.table.clearSelection();
    },
  },
};
</script>
<style lang="scss" scoped>
.info {
  padding: 10px 15px;
  margin-bottom: 15px;
  background: #e6f7ff;
  color: #1890ff;
  span {
    color: #565c5f;
  }
  .clear {
    margin-left: 20px;
    cursor: pointer;
  }
}
</style>
